package com.apkplug.imthemedemo.fragment;
import java.util.ArrayList;
import java.util.List;

import org.apkplug.app.FrameworkInstance;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import com.apkplug.imthemedemo.ProxyApplication;
import com.apkplug.imthemedemo.R;
import com.apkplug.imthemedemo.adapter.SearchBundleAdapter;
import com.apkplug.service.ApkplugCloudAgent;
import com.apkplug.service.SearchApp.AppSearchCallBack;
import com.apkplug.service.SearchApp.CallBackMSG;
import com.apkplug.service.SearchApp.appBean;
import com.apkplug.service.SearchApp.appSearch;
import com.apkplug.service.SearchApp.appSearchBean;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class fragment3 extends Fragment{
	private View mMainView;
	private FrameworkInstance frame=null;
	private appSearchBean bean=null;
	private SearchBundleAdapter adapter;
	private ListView bundlelist=null;
	private Button refresh=null;
    private ArrayList<appBean> apps = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Log.v("huahua", "fragment3-->onCreate()");
		
		LayoutInflater inflater = getActivity().getLayoutInflater();
		mMainView = inflater.inflate(R.layout.fragment3, (ViewGroup)getActivity().findViewById(R.id.viewpager), false);
		frame=((ProxyApplication)this.getActivity().getApplication()).getFrame();
		apps = new ArrayList<appBean>();
	    adapter = new SearchBundleAdapter(this.getActivity(),frame.getSystemBundleContext(),apps);
	    bundlelist=(ListView)mMainView.findViewById(R.id.bundlelist);
	    bundlelist.setAdapter(adapter);
	    refresh=(Button) mMainView.findViewById(R.id.refresh);
	    updataDate(frame.getSystemBundleContext());
	    refresh.setOnClickListener(
	    		new OnClickListener(){
					@Override
					public void onClick(View arg0) {
						updataDate(frame.getSystemBundleContext());
					}
	    });
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		Log.v("huahua", "fragment3-->onCreateView()");
		
		ViewGroup p = (ViewGroup) mMainView.getParent(); 
        if (p != null) { 
            p.removeAllViewsInLayout(); 
        } 
       
		return mMainView;
	}
	public void updataDate(BundleContext context){
		apps.clear();
		bean=new appSearchBean();
		//按插件被添加到分组(宿主)的时间逆序查询
    	bean.setG_order(appSearchBean.order_desc);
    	//插件的关键词keyword
    	//bean.setB_keywords("工具");
    	bean.setPagenum(10);
    	bean.setPage(0);
	    //查询
    	ApkplugCloudAgent.getAppSearch().search(bean,new impAppSearchCallBack());
	}
	
	/**
	 * 查询回调接口
	 * @author 梁前武
	 *
	 */
   class impAppSearchCallBack implements AppSearchCallBack{
	   public void onFailure(int arg0, final String arg1) {
		   //服务查询失败
		   getActivity().runOnUiThread(new Runnable(){
				public void run() {
					Toast.makeText(getActivity(), arg1,
				     Toast.LENGTH_SHORT).show();
				}
			});	
		}
		public void onSuccess(int stutas, CallBackMSG msg,appSearchBean arg2) {
			//服务查询成功
			if(stutas>=0){
				if(msg.getStutes()>=0){
					//获取查询到的插件
					final List<appBean> aps=(List)msg.getMsg();
					if(aps.size()==0){
						getActivity().runOnUiThread(new Runnable(){
							public void run() {
								Toast.makeText(getActivity(), "没有找到这种类型的插件",
							     Toast.LENGTH_SHORT).show();
							}
						});	
					}else{
						getActivity().runOnUiThread(new Runnable(){
							public void run() {
								Toast.makeText(getActivity(), "找到插件:"+aps.size(),
							     Toast.LENGTH_SHORT).show();
							}
						});	
					}
					for (int i = 0; i < aps.size(); i++) {
						appBean ab=aps.get(i);
   						apps.add(ab);
   				 	}
					//更新列表
					getActivity().runOnUiThread(new Runnable(){
   				 		public void run(){
   						 adapter.notifyDataSetChanged();
   				 		}
   				 	});
				}
			}else{
				getActivity().runOnUiThread(new Runnable(){
					public void run() {
						Toast.makeText(getActivity(), "网络连接错误",
					     Toast.LENGTH_SHORT).show();
					}
				});	
			}
		}  
   }
}

